<script lang="ts" context="module">
	import Data from "@graphite/components/panels/Data.svelte";
	import Document from "@graphite/components/panels/Document.svelte";
	import Layers from "@graphite/components/panels/Layers.svelte";
	import Properties from "@graphite/components/panels/Properties.svelte";

	const PANEL_COMPONENTS = {
		Document,
		Layers,
		Properties,
		Data,
	};
	type PanelType = keyof typeof PANEL_COMPONENTS;
</script>

<script lang="ts">
	import { getContext, tick } from "svelte";

	import type { Editor } from "@graphite/editor";
	import { type LayoutKeysGroup, type Key } from "@graphite/messages";
	import { platformIsMac, isEventSupported } from "@graphite/utility-functions/platform";

	import { extractPixelData } from "@graphite/utility-functions/rasterization";

	import LayoutCol from "@graphite/components/layout/LayoutCol.svelte";
	import LayoutRow from "@graphite/components/layout/LayoutRow.svelte";
	import IconButton from "@graphite/components/widgets/buttons/IconButton.svelte";
	import TextButton from "@graphite/components/widgets/buttons/TextButton.svelte";
	import IconLabel from "@graphite/components/widgets/labels/IconLabel.svelte";
	import TextLabel from "@graphite/components/widgets/labels/TextLabel.svelte";
	import UserInputLabel from "@graphite/components/widgets/labels/UserInputLabel.svelte";

	const BUTTON_MIDDLE = 1;

	const editor = getContext<Editor>("editor");

	export let tabMinWidths = false;
	export let tabCloseButtons = false;
	export let tabLabels: { name: string; unsaved?: boolean; tooltip?: string }[];
	export let tabActiveIndex: number;
	export let panelType: PanelType | undefined = undefined;
	export let clickAction: ((index: number) => void) | undefined = undefined;
	export let closeAction: ((index: number) => void) | undefined = undefined;

	let className = "";
	export { className as class };
	export let classes: Record<string, boolean> = {};
	let styleName = "";
	export { styleName as style };
	export let styles: Record<string, string | number | undefined> = {};

	let tabElements: (LayoutRow | undefined)[] = [];

	function platformModifiers(reservedKey: boolean): LayoutKeysGroup {
		// TODO: Remove this by properly feeding these keys from a layout provided by the backend

		const ALT: Key = { key: "Alt", label: "Alt" };
		const COMMAND: Key = { key: "Command", label: "Command" };
		const CONTROL: Key = { key: "Control", label: "Ctrl" };

		if (platformIsMac()) return reservedKey ? [ALT, COMMAND] : [COMMAND];
		return reservedKey ? [CONTROL, ALT] : [CONTROL];
	}

	function dropFile(e: DragEvent) {
		if (!e.dataTransfer) return;

		e.preventDefault();

		Array.from(e.dataTransfer.items).forEach(async (item) => {
			const file = item.getAsFile();
			if (!file) return;

			if (file.type.includes("svg")) {
				const svgData = await file.text();
				editor.handle.pasteSvg(file.name, svgData);
				return;
			}

			if (file.type.startsWith("image")) {
				const imageData = await extractPixelData(file);
				editor.handle.pasteImage(file.name, new Uint8Array(imageData.data), imageData.width, imageData.height);
				return;
			}

			const graphiteFileSuffix = "." + editor.handle.fileExtension();
			if (file.name.endsWith(graphiteFileSuffix)) {
				const content = await file.text();
				const documentName = file.name.slice(0, -graphiteFileSuffix.length);
				editor.handle.openDocumentFile(documentName, content);
				return;
			}
		});
	}

	export async function scrollTabIntoView(newIndex: number) {
		await tick();
		tabElements[newIndex]?.div?.()?.scrollIntoView();
	}
</script>

<LayoutCol on:pointerdown={() => panelType && editor.handle.setActivePanel(panelType)} class={`panel ${className}`.trim()} {classes} style={styleName} {styles}>
	<LayoutRow class="tab-bar" classes={{ "min-widths": tabMinWidths }}>
		<LayoutRow class="tab-group" scrollableX={true}>
			{#each tabLabels as tabLabel, tabIndex}
				<LayoutRow
					class="tab"
					classes={{ active: tabIndex === tabActiveIndex }}
					tooltip={tabLabel.tooltip || undefined}
					on:click={(e) => {
						e.stopPropagation();
						clickAction?.(tabIndex);
					}}
					on:auxclick={(e) => {
						// Middle mouse button click
						if (e.button === BUTTON_MIDDLE) {
							e.stopPropagation();
							closeAction?.(tabIndex);
						}
					}}
					on:mouseup={(e) => {
						// Middle mouse button click fallback for Safari:
						// https://developer.mozilla.org/en-US/docs/Web/API/Element/auxclick_event#browser_compatibility
						// The downside of using mouseup is that the mousedown didn't have to originate in the same element.
						// A possible future improvement could save the target element during mousedown and check if it's the same here.
						if (!isEventSupported("auxclick") && e.button === BUTTON_MIDDLE) {
							e.stopPropagation();
							closeAction?.(tabIndex);
						}
					}}
					bind:this={tabElements[tabIndex]}
				>
					<LayoutRow class="name">
						<TextLabel class="text">{tabLabel.name}</TextLabel>
						{#if tabLabel.unsaved}
							<TextLabel>*</TextLabel>
						{/if}
					</LayoutRow>
					{#if tabCloseButtons}
						<IconButton
							action={(e) => {
								e?.stopPropagation();
								closeAction?.(tabIndex);
							}}
							icon="CloseX"
							size={16}
						/>
					{/if}
				</LayoutRow>
			{/each}
		</LayoutRow>
		<!-- <PopoverButton style="VerticalEllipsis">
			<TextLabel bold={true}>Panel Options</TextLabel>
			<TextLabel multiline={true}>Coming soon</TextLabel>
		</PopoverButton> -->
	</LayoutRow>
	<LayoutCol class="panel-body">
		{#if panelType}
			<svelte:component this={PANEL_COMPONENTS[panelType]} />
		{:else}
			<LayoutCol class="empty-panel" on:dragover={(e) => e.preventDefault()} on:drop={dropFile}>
				<LayoutCol class="top-spacer"></LayoutCol>
				<LayoutCol class="content-container">
					<LayoutCol class="content">
						<LayoutRow class="logotype">
							<IconLabel icon="GraphiteLogotypeSolid" />
						</LayoutRow>
						<LayoutRow class="actions">
							<table>
								<tbody>
									<tr>
										<td>
											<TextButton label="New Document" icon="File" flush={true} action={() => editor.handle.newDocumentDialog()} />
										</td>
										<td>
											<UserInputLabel keysWithLabelsGroups={[[...platformModifiers(true), { key: "KeyN", label: "N" }]]} />
										</td>
									</tr>
									<tr>
										<td>
											<TextButton label="Open Document" icon="Folder" flush={true} action={() => editor.handle.openDocument()} />
										</td>
										<td>
											<UserInputLabel keysWithLabelsGroups={[[...platformModifiers(false), { key: "KeyO", label: "O" }]]} />
										</td>
									</tr>
									<tr>
										<td colspan="2">
											<TextButton label="Open Demo Artwork" icon="Image" flush={true} action={() => editor.handle.demoArtworkDialog()} />
										</td>
									</tr>
									<tr>
										<td colspan="2">
											<TextButton label="Support the Development Fund" icon="Heart" flush={true} action={() => editor.handle.visitUrl("https://graphite.rs/donate/")} />
										</td>
									</tr>
								</tbody>
							</table>
						</LayoutRow>
					</LayoutCol>
				</LayoutCol>
				<LayoutCol class="bottom-message">
					{#if new Date().getFullYear() === 2025}
						<TextLabel italic={true} disabled={true}>
							September 2025 release — <a href="https://youtube.com/watch?v=Vl5BA4g3QXM" target="_blank">What's new? (video)</a>
							— Note: some older documents may render differently and require manual fixes.
							<a href="https://ec6796b4.graphite-editor.pages.dev/" target="_blank">Need the old version?</a>
						</TextLabel>
					{/if}
				</LayoutCol>
			</LayoutCol>
		{/if}
	</LayoutCol>
</LayoutCol>

<style lang="scss" global>
	.panel {
		background: var(--color-1-nearblack);
		border-radius: 6px;
		overflow: hidden;

		.tab-bar {
			height: 28px;
			min-height: auto;
			background: var(--color-1-nearblack); // Needed for the viewport hole punch on desktop
			flex-shrink: 0;

			&.min-widths .tab-group .tab {
				min-width: 120px;
				max-width: 360px;
			}

			.tab-group {
				flex: 1 1 100%;
				position: relative;

				// This always hangs out at the end of the last tab, providing 16px (15px plus the 1px reserved for the separator line) to the right of the tabs.
				// When the last tab is selected, its bottom rounded fillet adds 16px to the width, which stretches the scrollbar width allocation in only that situation.
				// This pseudo-element ensures we always reserve that space to prevent the scrollbar from jumping when the last tab is selected.
				// There is unfortunately no apparent way to remove that 16px gap from the end of the scroll container, since negative margin does not reduce the scrollbar allocation.
				&::after {
					content: "";
					width: 15px;
					flex: 0 0 auto;
				}

				.tab {
					flex: 0 1 auto;
					height: 28px;
					padding: 0 8px;
					align-items: center;
					position: relative;

					&.active {
						background: var(--color-3-darkgray);
						border-radius: 6px 6px 0 0;
						position: relative;

						&:not(:first-child)::before,
						&::after {
							content: "";
							width: 16px;
							height: 8px;
							position: absolute;
							bottom: 0;
						}

						&:not(:first-child)::before {
							left: -16px;
							border-bottom-right-radius: 8px;
							box-shadow: 8px 0 0 0 var(--color-3-darkgray);
						}

						&::after {
							right: -16px;
							border-bottom-left-radius: 8px;
							box-shadow: -8px 0 0 0 var(--color-3-darkgray);
						}
					}

					.name {
						flex: 1 1 100%;

						.text-label {
							// Height and line-height required because https://stackoverflow.com/a/21611191/775283
							height: 28px;
							line-height: 28px;
							flex: 0 0 auto;

							&.text {
								overflow-x: hidden;
								white-space: nowrap;
								text-overflow: ellipsis;
								flex-shrink: 1;
							}
						}
					}

					.icon-button {
						margin-left: 8px;
					}

					& + .tab {
						margin-left: 1px;
					}

					&:not(.active) + .tab:not(.active)::before {
						content: "";
						position: absolute;
						left: -1px;
						width: 1px;
						height: 16px;
						background: var(--color-4-dimgray);
					}

					&:last-of-type {
						margin-right: 1px;

						&:not(.active)::after {
							content: "";
							position: absolute;
							right: -1px;
							width: 1px;
							height: 16px;
							background: var(--color-4-dimgray);
						}
					}
				}
			}

			// .popover-button {
			// 	margin: 2px 4px;
			// }
		}

		.panel-body {
			background: var(--color-3-darkgray);
			flex: 1 1 100%;
			flex-direction: column;

			> div {
				padding-bottom: 4px;
			}

			.empty-panel {
				background: var(--color-2-mildblack);
				margin: 4px;
				border-radius: 2px;
				justify-content: space-between;

				.content-container {
					flex: 0 0 auto;
					justify-content: center;

					.content {
						flex: 0 0 auto;
						align-items: center;

						.logotype {
							margin-top: 8px;
							margin-bottom: 40px;

							svg {
								width: auto;
								height: 120px;
							}
						}

						.actions {
							margin-bottom: 8px;

							table {
								border-spacing: 8px;
								margin: -8px;

								td {
									padding: 0;
								}
							}
						}
					}
				}

				.top-spacer {
					flex: 0 1 48px;
				}

				.bottom-message {
					flex: 0 0 48px;
					align-items: center;
					justify-content: end;

					.text-label {
						white-space: wrap;
						margin: 0 1em;
					}
				}
			}
		}

		// Needed for the viewport hole punch on desktop
		.viewport-hole-punch &.document-panel,
		.viewport-hole-punch &.document-panel .panel-body:not(:has(.empty-panel)) {
			background: none;
		}
	}
</style>
